package com.example.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.domain.StudentClass;
import com.example.result.Result;
import com.example.service.StudentClassService;
import com.example.tool.GsonTool;

/**
 * @author 袁小弘
 * @describe 学生班级控制层
 * @date 2020-02-18
 */
@RestController
@RequestMapping("/api")
public class StudentClassController {

    @Autowired
    StudentClassService studentClassService;

    // ===============================学生班级管理==============================================

    /**
     * @param json
     * @return 添加成功则提示成功信息，反之则提示失败信息
     * @describe 添加学生班级信息
     */
    @RequestMapping(value = "/studentClass", method = RequestMethod.POST)
    public Result addStudentClass(@RequestBody String json) {


        return studentClassService.addStudentClass(json);

    }

    /**
     * @param json
     * @return 更新成功则提示成功信息，反之则提示失败信息
     * @describe 更新学生班级信息
     */
    @RequestMapping(value = "/studentClass", method = RequestMethod.PUT)
    public Result updatestudentClass(@RequestBody String json) {
        // 将JSON数据转换为studentClass对象实体
        JSONObject parseObject = JSON.parseObject(json);
        StudentClass studentClass = GsonTool.getGsonInstance().fromJson(parseObject.getString("json"),
                StudentClass.class);
        return studentClassService.updateStudentClass(studentClass);

    }

    /**
     * @param studentClassId
     * @return 删除成功则提示成功信息，反之则提示失败信息
     * @describe 根据学生班级编号删除学生班级信息
     */
    @RequestMapping(value = "/studentClass/{studentClassId}", method = RequestMethod.DELETE)
    public Result deleteStudentClassById(@PathVariable("studentClassId") String studentClassId) {
        return studentClassService.deleteStudentClassById(studentClassId);

    }

    /**
     * @return 以JSON格式返回所有班级信息
     * @describe 查询所有学生班级信息
     */
    @RequestMapping(value = "/studentClass", method = RequestMethod.GET)
    public Result queryAllStudentClass() {
        return studentClassService.queryAllStudentClass();

    }

    /**
     * @param studentClassId
     * @return 以JSON格式返回该学生班级信息
     * @describe 根据编号查询学生班级信息
     */
    @RequestMapping(value = "/studentClass/{studentClassId}", method = RequestMethod.GET)
    public Result queryStudentClassById(@PathVariable("studentClassId") String studentClassId) {
        return studentClassService.queryStudentClassById(studentClassId);

    }

    /**
     * @param json
     * @return 添加成功则返回一个成功提示，反之则提示失败信息
     * @describe 通过Excel批量添加学生班级信息
     */
    @RequestMapping(value = "/studentClass/excels", method = RequestMethod.POST)
    public Result addStudentClassByExcel(@RequestBody String json) {
        System.out.println(json);
        JSONObject parseObject = JSON.parseObject(json);
        List<StudentClass> list = JSON.parseArray(parseObject.getString("json"), StudentClass.class);

        System.out.println(list);

        return studentClassService.addStudentClassByExcel(list);

    }

    /**
     * @param Ids
     * @return 删除成功则返回一个成功提示，反之则提示失败信息
     * @describe 根据学生班级编号批量删除学生班级信息
     */
    @RequestMapping(value = "/studentClass/dels", method = RequestMethod.POST)
    public Result deleteStudentClassByIds(@RequestBody Map<String, String> Ids) {

        return studentClassService.deleteStudentClassByIds(Ids);
    }

    /**
     * @param json
     * @return 查询成功则返回一个成功提示，反之则提示失败信息
     * @describe 学生班级信息模糊查询
     */
    @RequestMapping(value = "/studentClass/serachs", method = RequestMethod.POST)
    public Result serachByInfo(@RequestBody String json) {
        System.out.println(json);
        JSONObject parseObject = JSON.parseObject(json);
        String[] info = parseObject.getString("json").split(",");

        return studentClassService.serachByInfo(info);

    }

    /**
     * @return 查询成功则返回一个成功提示，反之则提示失败信息
     * @describe 查询所有未分配班级的学生
     */
    @RequestMapping(value = "/studentClass/getStudents", method = RequestMethod.GET)
    public Result queryAllStudents() {
        return studentClassService.queryAllStudents();

    }

    /**
     * @return 查询成功则返回一个成功提示，反之则提示失败信息
     * @describe 查询学生已经分配班级的班级名称
     */
    @RequestMapping(value = "/studentClass/getClass", method = RequestMethod.GET)
    public Result queryAllClass() {
        return studentClassService.queryAllClass();

    }

    /**
     * @return 查询成功则返回一个成功提示，反之则提示失败信息
     * @describe 查询所有的班级名称
     */
    @RequestMapping(value = "/studentClass/getAllClass", method = RequestMethod.GET)
    public Result queryAllClassInfo() {
        return studentClassService.queryAllClassInfo();

    }

    /**
     * @return 查询成功则返回一个成功提示，反之则提示失败信息
     * @describe 查询所有学生和班级的已分配信息
     */
    @RequestMapping(value = "/studentClass/studentAndClass", method = RequestMethod.GET)
    public Result queryAllStudentAndClassInfo() {
        return studentClassService.queryAllStudentAndClassInfo();
    }

}
